package ex2.employee;



public class CEmployee implements IEmployee {

	private String _username;
	private String _firstName;
	private String _surname;
	private double _salary;
	private String _code;
	
	protected String _extraData = null;
	protected IEmployeeHandler _employeeHandler;
	
	public CEmployee(String record, IEmployeeHandler employeeHandler) throws IllegalArgumentException{
		String tLeftRecord;
		this._employeeHandler = employeeHandler;
		try {
		//employee code parsing
			this._code = _parser(record);	
			tLeftRecord = (record.substring(this._code.length()+1)).trim();
		
			//user name parsing
			this._username = _parser(tLeftRecord);
			if(!this._employeeHandler.isAvailableUsername(this._username)) 
				throw new IllegalArgumentException();
			tLeftRecord = (tLeftRecord.substring(this._username.length()+1)).trim();

		
			//first name parsing		
			this._firstName = _parser(tLeftRecord);
			tLeftRecord = (tLeftRecord.substring(this._firstName.length()+1)).trim();
		
			//surname parsing
			this._surname = _parser(tLeftRecord);
			tLeftRecord = (tLeftRecord.substring(this._surname.length()+1)).trim();
			
			//salary parsing	
			double tParsedSalary = 0.0;
			String tStringParsedSalary = _parser(tLeftRecord);
			try{
				tParsedSalary = Double.parseDouble(tStringParsedSalary);
			
			}
			catch (NumberFormatException e){
				throw new IllegalArgumentException();
			}
		
			this._salary = tParsedSalary;
			if(this._salary < 0) throw new IllegalArgumentException();
			if (tStringParsedSalary.length()+1 < tLeftRecord.length())
				this._extraData = (tLeftRecord.substring(tStringParsedSalary.length()+1)).trim();
		} 
		catch (StringIndexOutOfBoundsException e){
			throw new IllegalArgumentException();
		}
			
	}
	
	
	@Override
	public String getFirstName() {
		return this._firstName;
	}

	@Override
	public String getLastName() {
		return this._surname;
	}

	@Override
	public double calaulateTotalExpenses() {
		return this._salary;
	}
	
	
	@Override
	public double getSalary(){
		return this._salary;
	}
	
	@Override
	public String getUsername(){
		return this._username;
	}

	public String getEmployeeCode(){
		return this._code;
	}
	
	/**
	 * 
	 * @param record
	 * @return return string token, each token is separated by whiteSpace.
	 */
	protected String _parser(String record){
		
		String tTrimmedRecord = record.trim();
		int tIndexOfSeperator = tTrimmedRecord.indexOf(" ");
		String tAnswer;
		
		if (tIndexOfSeperator == -1)
			tAnswer = tTrimmedRecord.substring(0);
		else
			tAnswer = tTrimmedRecord.substring(0,tIndexOfSeperator);
		
		return tAnswer;
	}


	@Override
	public String getSubordinates() {
		return null;
	}


	@Override
	public String getEquipment() {
		return null;
	}





	@Override
	public void deleteRedundantData() {
		
	}
}
